package com.wanxi.app.service.user;

import com.wanxi.app.controller.api.AddressApi;
import com.wanxi.app.controller.coupon.CouponAppController;
import com.wanxi.app.domain.vo.MyShoppingVo;
import com.wanxi.app.domain.vo.ShoppingCartVo;
import com.wanxi.coupon.api.query.CouponUserQuery;
import com.wanxi.coupon.api.vo.CouponUserVo;
import com.wanxi.product.service.api.feign.ProductUserService;
import com.wanxi.product.service.api.result.CommonResult;
import com.wanxi.product.service.api.vo.user.SkuFindByIdOutVo;
import com.wanxi.user.api.ShoppingCartFeign;
import com.wanxi.user.domain.dto.DelShoppingCartDto;
import com.wanxi.user.domain.dto.ShoppingCartDto;
import com.wanxi.user.domain.entity.Address;
import com.wanxi.user.domain.entity.ShoppingCart;
import com.wanxi.user.domain.result.UserResult;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author:wgy
 * @Data：2022/1/13 10:03
 */
@Service
public class MyShoppingService {

    @Resource
    private ProductUserService productUserService;

    @Resource
    private ShoppingCartFeign shoppingCartFeign;

    public CommonResult<MyShoppingVo> findMyShopping(long userId){
//        user服务中的购物车信息
        UserResult<List<ShoppingCart>> cartProduct = shoppingCartFeign.findCartProduct(userId);
        List<ShoppingCart> data = cartProduct.getData();
        List<ShoppingCartVo> productList = new ArrayList<>();
        if (!data.isEmpty()){
            int i=0;
//            进入商品服务和商家服务查询商品信息和店铺信息
            for (ShoppingCart datum : data) {
                new ShoppingCartVo();
//              查询商品信息
                long skuId = (long)datum.getSkuId();
                Long shopId = datum.getShopId();
                Long aLong = Long.valueOf(skuId);
                CommonResult<SkuFindByIdOutVo> skuById = productUserService.findSkuById(aLong);

//                查询店铺信息
                String shopName= "店铺名称";

                if (skuById.getCode()==200){
                    productList.add(new ShoppingCartVo(
                            data.get(i).getSkuId(),
                            skuById.getData(),
                            data.get(i).getProductNum(),
                            data.get(i).getShopId(),
                            shopName,
                            data.get(i++).getCreateTime()
                    ));
                }
            }
            MyShoppingVo myShoppingVo = MyShoppingVo.builder()
                    .userId(userId)
                    .shoppingCartVos(productList)
                    .build();
            return CommonResult.success(myShoppingVo);
        }
        return CommonResult.success();

    }

    public UserResult<String> delShoppingCartByUserIdAndSkuId(DelShoppingCartDto shoppingCartDto) {
        UserResult<String> stringUserResult = shoppingCartFeign.delShoppingCartByUserIdAndSkuId(shoppingCartDto);
        if (stringUserResult.getCode()==200){
            return stringUserResult;
        }
        return UserResult.failed("删除失败");
    }

    public UserResult<Boolean> delCartProductAll(long userId) {
        UserResult<Boolean> booleanUserResult = shoppingCartFeign.delCartProductAll(userId);
        if (booleanUserResult.getData()){
            return booleanUserResult;
        }
        return UserResult.failed("删除失败");
    }

    public UserResult<Long> addCartProduct(ShoppingCartDto productCartDto) {
        UserResult<Long> integerUserResult = shoppingCartFeign.addCartProduct(productCartDto);
        if (integerUserResult.getData()>0){
            return integerUserResult;
        }
        return UserResult.failed();

    }

    public UserResult<BigDecimal> calculationAmount(Long userId) {
        BigDecimal allAmount = BigDecimal.valueOf(0);
        MyShoppingVo myShopping = findMyShopping(userId).getData();
        for (ShoppingCartVo productVo : myShopping.getShoppingCartVos()) {
            SkuFindByIdOutVo skuVo = productUserService.findSkuById(productVo.getSkuId()).getData();
            allAmount = allAmount.add(skuVo.getSalePrice().multiply(BigDecimal.valueOf(productVo.getNum())));
        }
        return UserResult.success(allAmount);
    }
}
